/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifpb.pibict.siscoord.persistence;


import java.util.Collection;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DAOJPA implements DAO {

    private EntityManagerFactory ef;
    private EntityManager em;
    private Query query;

    public DAOJPA() {
    }

    public Boolean salvar(Object obj) throws Exception {
        try {
            open();
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();
            return true;
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return false;
        } finally {
//            this.close();
        }

    }

    public Boolean remover(Object obj) throws Exception {
        try {
            open();
            em.getTransaction().begin();
            em.remove(obj);
            em.getTransaction().commit();
            return true;
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return false;
        } finally {
//            this.close();
        }
    }

    public Boolean atualizar(Object obj) throws Exception {
        try {
            open();
            em.getTransaction().begin();
            em.merge(obj);            
            em.getTransaction().commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            ex.printStackTrace();
            return false;
        } finally {
//            this.close();
        }

    }

    public <T> Collection<T> buscarTodos(Class<T> classe) throws Exception {
        try {
            open();
            em.getTransaction().begin();
            query = em.createQuery("from " + classe.getSimpleName() + " t");
            Collection<T> t2 = query.getResultList();
            em.getTransaction().commit();
            return t2;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return null;
        } finally {
            this.close();
        }

    }

    public <T> T buscar(Class<T> classe, int id) throws Exception {
        try {
            open();
            return em.find(classe, id);
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
            return null;
        } finally {
//            close();
        }
    }

    public <T> Collection<T> executeQuery(Class<T> classe, String queryCondicao, Map<Integer, Object> param) throws Exception {
        try {
            open();
            this.query = em.createQuery("from " + classe.getSimpleName() + " o " + queryCondicao);
            for (Integer key : param.keySet()) {
                this.query.setParameter(key, param.get(key));
            }
            return this.query.getResultList();
        } finally {
//            close();
        }
    }

    public <T> Collection<T> consultaList(String query, Map<Integer, Object> param) throws Exception {

        try {
            open();
            this.query = em.createNamedQuery(query);
            for (Integer key : param.keySet()) {
                this.query.setParameter(key, param.get(key));
            }
            return this.query.getResultList();
        } finally {
//            close();
        }

    }

    public <T> T consultaSimples(String query, Map<Integer, Object> param) throws Exception {
        
        Object o = null;
        try {
            open();
            this.query = em.createNamedQuery(query);            
            for (Integer key : param.keySet()) {
                this.query.setParameter(key, param.get(key));
            }            
            o = this.query.getSingleResult();            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return (T) o;
    }

    private void close() {
        if (this.em != null && this.em.isOpen()) {
            this.em.close();
        }
        if (this.ef != null && this.ef.isOpen()) {
            this.ef.close();
        }

    }

    protected void open() {        
        if (this.ef == null || !ef.isOpen()) {
            this.ef = Persistence.createEntityManagerFactory("SISCOORDPU");
        }
        if (em == null || !em.isOpen()) {
            this.em = ef.createEntityManager();
        }    

    }
}
